{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0, 0.5, 'y')"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 1准备数据\n",
    "#1.准备数据y=3x+0.8,准备参数\n",
    "import torch\n",
    "x=torch.rand([50])#x是随机的50个数据 x在0-1之间\n",
    "y=3*x+0.8#y是x的函数 也是50个数据\n",
    "\n",
    "#可视化一下\n",
    "import matplotlib.pyplot as plt\n",
    "plt.figure()\n",
    "plt.scatter(x,y)\n",
    "plt.xlabel('x')\n",
    "plt.ylabel('y')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([0.6797], requires_grad=True)\n",
      "tensor([0.5042], requires_grad=True)\n"
     ]
    }
   ],
   "source": [
    "#准备一下要去拟合的线性回归的w和b\n",
    "w=torch.rand(1,requires_grad=True)#随机初始的一个w 范围在0-1之间\n",
    "b=torch.rand(1,requires_grad=True)#随机初始的一个b 范围在0-1之间\n",
    "print(w)\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 准备一下函数：计算损失，把参数的梯度设置为0，进行反向传播\n",
    "def loss_fn(y,y_predict):\n",
    "    loss=(y_predict-y).pow(2).mean()#均方误差\n",
    "    #每次反向传播前把梯度置为0\n",
    "    for i in [w,b]:\n",
    "        #w和b中\n",
    "        if i.grad is not None:#如果梯度不为空 就设置为0\n",
    "            i.grad.data.zero_()\n",
    "    loss.backward()#反向传播\n",
    "    return loss.data#返回loss的具体数值\n",
    "\n",
    "## 准备函数：更新参数\n",
    "def optimize(learning_rate):\n",
    "    w.data-=learning_rate*w.grad.data#这里运用的是梯度下降法\n",
    "    b.data-=learning_rate*b.grad.data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/conda/lib/python3.6/site-packages/torch/autograd/__init__.py:132: UserWarning: CUDA initialization: Found no NVIDIA driver on your system. Please check that you have an NVIDIA GPU and installed a driver from http://www.nvidia.com/Download/index.aspx (Triggered internally at  /pytorch/c10/cuda/CUDAFunctions.cpp:100.)\n",
      "  allow_unreachable=True)  # allow_unreachable flag\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "迭代次数: 0 \t误差： tensor(2.5866)\n",
      "迭代次数: 300 \t误差： tensor(0.1086)\n",
      "迭代次数: 600 \t误差： tensor(0.0486)\n",
      "迭代次数: 900 \t误差： tensor(0.0217)\n",
      "迭代次数: 1200 \t误差： tensor(0.0097)\n",
      "迭代次数: 1500 \t误差： tensor(0.0043)\n",
      "迭代次数: 1800 \t误差： tensor(0.0019)\n",
      "迭代次数: 2100 \t误差： tensor(0.0009)\n",
      "迭代次数: 2400 \t误差： tensor(0.0004)\n",
      "迭代次数: 2700 \t误差： tensor(0.0002)\n"
     ]
    }
   ],
   "source": [
    "for i in range(3000):\n",
    "    ## 2计算预测值\n",
    "    y_predict=x*w+b\n",
    "    ## 3计算损失 并把参数的梯度置为0 进行反向传播\n",
    "    loss=loss_fn(y,y_predict)   \n",
    "    \n",
    "    if i%300==0:\n",
    "        print('迭代次数:',i,'\\t误差：',loss)\n",
    "    ## 4更新参数w和b\n",
    "    optimize(0.01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "w: tensor([2.9700], requires_grad=True)\n",
      "b tensor([0.8161], requires_grad=True)\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYIAAAEGCAYAAABo25JHAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMywgaHR0cHM6Ly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f51xk8wa5kBg1gTFujc/JtkD4T9HxO6Mm2TWMHcNmTXHfJI6T4eRZAZmHcMZgVkTSFpDslvWq4FFEfGBjJtk1rCOqT5q1q4kXQjsjIivSpoD3CfplIj4XtZtM2uEMwIzs5zzGIGZWc45EJiZ5ZwDgZlZzjkQmJnlnAOBmVnOORCYmeWcA4GZWc79f0TmxJ5nR5CDAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "## 5效果可视化\n",
    "y_predict=w*x+b #训练好的w和b  然后算出y的预测值\n",
    "\n",
    "plt.figure()\n",
    "plt.scatter(x,y,c='b')#原来的数据用散点图 颜色是蓝色\n",
    "plt.plot(x,y_predict.detach().numpy(),c='r')#拟合的数据用直线图  颜色是红色\n",
    "plt.xlabel('x')\n",
    "plt.ylabel('y')\n",
    "print('w:',w)\n",
    "print('b',b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    " "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
